package simulator.enums;

public enum Direction
{
	NORTH("N", 0),
	EAST("E", 1),
	SOUTH("S", 2),
	WEST("W", 3);
	
	private String	_specStr;
	private int		_id;
	
	private Direction(String specStr, int id)
	{
		_specStr = specStr;
		_id = id;
	}
	
	public String getName()
	{
		return _specStr;
	}
	
	public int getID()
	{
		return _id;
	}
	
	public static Direction getLeft(Direction d)
	{
		return getFromID((d._id + 1) % 4);
	}
	
	public static Direction getRight(Direction d)
	{
		return getFromID((d._id - 1) % 4);
	}
	
	public static Direction getStraight(Direction d)
	{
		return getFromID((d._id + 2) % 4);
	}
	
	public static Direction getFromString(String str)
	{
		if("north".equalsIgnoreCase(str) || "n".equalsIgnoreCase(str))
			return Direction.NORTH;
		if("east".equalsIgnoreCase(str) || "e".equalsIgnoreCase(str) || "o".equalsIgnoreCase(str)) /* old spec version contained 'EAST = O' */
			return Direction.EAST;
		if("south".equalsIgnoreCase(str) || "s".equalsIgnoreCase(str))
			return Direction.SOUTH;
		if("west".equalsIgnoreCase(str) || "w".equalsIgnoreCase(str))
			return Direction.WEST;
		
		return null;
	}
	
	public static Direction getFromChar(char ch)
	{
		switch(ch)
		{
			case 'n':
			case 'N':
				return Direction.NORTH;
			case 'e':
			case 'E':
			case 'o':
			case 'O':
				return Direction.EAST;
			case 's':
			case 'S':
				return Direction.SOUTH;
			case 'w':
			case 'W':
				return Direction.WEST;
			default:
				return null;
		}
	}
	
	public static Direction getFromID(int id)
	{
		Direction[] vals = Direction.values();
		
		for(int i=0; i<vals.length; i++)
		{
			if(vals[i]._id == id) return vals[i];
		}
		
		return null;
	}
}
